package com.shuqi.model;

import java.util.ArrayList;
import java.util.List;

import org.apache.http.message.BasicNameValuePair;

import android.content.Context;

import com.shuqi.common.MyTask;
import com.shuqi.common.Urls;
import com.shuqi.common.utils.Log4an;
import com.shuqi.database.model.UserInfo;
import com.shuqi.model.manager.MyAccountManager;
import com.shuqi.model.net.HttpRequest;
import com.shuqi.model.net.UrlManager;
import com.shuqi.model.net.listener.HttpRequestListener;
import com.shuqi.model.parse.parser.PayAlipayParser;
import com.shuqi.model.parse.parser.PayFormParser;
import com.shuqi.model.parse.parser.PayParser;
import com.shuqi.model.parse.parser.PayTypeParser;

/**
 * 支付数据
 * 
 * @author chenwei
 * 
 */
public class PayModel extends ModelBase implements HttpRequestListener {

    private final static String TAG = "PayModel";
    
    public final static String NET_SUCCESS = "200";
    public final static String NET_NEEDLOGIN = "201";

    private HttpRequest httpRequest;

    private Context context;

    private UserInfo userInfo;

    private final static String KEY = "358ea1a90b612d619bcf0d6a85ae147b";

    public PayModel(Context context) {
        this.context = context;
        userInfo = MyAccountManager.getInstance(context);
    }

    @Override
    public void action(int actionCode, Object object) {
        Log4an.e(TAG, "action:" + actionCode);
        fireEvent(actionCode, object);
    }

    @Override
    public List<BasicNameValuePair> setParams() {
        return null;
    }

    @Override
    public String getRequestUrl() {
        return Urls.getPayUrl();
    }

    @Override
    public void onDestroy() {
        if (httpRequest != null) {
            httpRequest.abort();
        }
    }

    @Override
    public void loadData(final Object... objs) {
    }

    public void getPayModes() {

        Log4an.e(TAG, "获取充值首页数据");

        Runnable runnable = new Runnable() {
            @Override
            public void run() {
                String uid = userInfo.getUserId();
                long timestamp = System.currentTimeMillis();

                StringBuilder paramSb = new StringBuilder();
                paramSb.append("timestamp=").append(timestamp).append("&");
                paramSb.append("user_id=").append(uid);

                Log4an.d(TAG, paramSb.toString());

                String sign = Log4an.MD5(paramSb.toString() + KEY);

                List<BasicNameValuePair> params = new ArrayList<BasicNameValuePair>();
                params.add(new BasicNameValuePair("timestamp", timestamp + ""));
                params.add(new BasicNameValuePair("user_id", uid));

                params.add(new BasicNameValuePair("sign", sign));

                httpRequest =
                        new HttpRequest(context, UrlManager.COLUMN_DOMAIN_PAY, getRequestUrl(), params,
                                PayModel.this);
                httpRequest.setDataParseAdapter(new PayParser());
                MyTask.runInBackground(httpRequest, true);
            }
        };
        MyTask.runInBackground(runnable, true);
    }

    public void getPayDatasByModeId(final String modeId) {
        Runnable runnable = new Runnable() {
            @Override
            public void run() {
                String uid = userInfo.getUserId();
                long timestamp = System.currentTimeMillis();

                StringBuilder paramSb = new StringBuilder();
                paramSb.append("modeId=").append(modeId).append("&");
                paramSb.append("timestamp=").append(timestamp).append("&");
                paramSb.append("user_id=").append(uid);

                Log4an.d(TAG, paramSb.toString());

                String sign = Log4an.MD5(paramSb.toString() + KEY);

                List<BasicNameValuePair> params = new ArrayList<BasicNameValuePair>();
                params.add(new BasicNameValuePair("user_id", uid));
                params.add(new BasicNameValuePair("modeId", modeId));
                params.add(new BasicNameValuePair("timestamp", timestamp + ""));

                params.add(new BasicNameValuePair("sign", sign));

                httpRequest =
                        new HttpRequest(context, UrlManager.COLUMN_DOMAIN_PAY, getRequestUrl(), params,
                                PayModel.this);
                httpRequest.setDataParseAdapter(new PayTypeParser());
                MyTask.runInBackground(httpRequest, true);
            }
        };
        MyTask.runInBackground(runnable, true);
    }

    public void pay(final String cardNo, final String cardPwd, final String modeId,
            final String typeId, final String itemId) {

        Log4an.d(TAG, "充值");

        Runnable runnable = new Runnable() {
            @Override
            public void run() {
                String uid = userInfo.getUserId();
                long timestamp = System.currentTimeMillis();

                StringBuilder paramSb = new StringBuilder();
                paramSb.append("cardNo=").append(cardNo).append("&");
                paramSb.append("cardPwd=").append(cardPwd).append("&");
                paramSb.append("itemId=").append(itemId).append("&");
                paramSb.append("modeId=").append(modeId).append("&");
                paramSb.append("timestamp=").append(timestamp).append("&");
                paramSb.append("typeId=").append(typeId).append("&");
                paramSb.append("user_id=").append(uid);

                Log4an.d(TAG, paramSb.toString());

                String sign = Log4an.MD5(paramSb.toString() + KEY);

                List<BasicNameValuePair> params = new ArrayList<BasicNameValuePair>();
                params.add(new BasicNameValuePair("cardNo", cardNo));
                params.add(new BasicNameValuePair("cardPwd", cardPwd));
                params.add(new BasicNameValuePair("itemId", itemId));
                params.add(new BasicNameValuePair("modeId", modeId));
                params.add(new BasicNameValuePair("timestamp", timestamp + ""));
                params.add(new BasicNameValuePair("typeId", typeId));
                params.add(new BasicNameValuePair("user_id", uid));
                params.add(new BasicNameValuePair("sign", sign));

                httpRequest =
                        new HttpRequest(context, UrlManager.COLUMN_DOMAIN_PAY, getRequestUrl(), params,
                                PayModel.this);
                httpRequest.setDataParseAdapter(new PayFormParser());
                MyTask.runInBackground(httpRequest, true);
            }
        };
        MyTask.runInBackground(runnable, true);

    }

    public void getPayInfo(final String modeId, final String typeId, final String itemId) {
        Log4an.d(TAG, "充值");

        Runnable runnable = new Runnable() {
            @Override
            public void run() {
                String uid = userInfo.getUserId();
                long timestamp = System.currentTimeMillis();

                StringBuilder paramSb = new StringBuilder();
                paramSb.append("itemId=").append(itemId).append("&");
                paramSb.append("modeId=").append(modeId).append("&");
                paramSb.append("timestamp=").append(timestamp).append("&");
                paramSb.append("typeId=").append(typeId).append("&");
                paramSb.append("user_id=").append(uid);

                Log4an.d(TAG, paramSb.toString());

                String sign = Log4an.MD5(paramSb.toString() + KEY);

                List<BasicNameValuePair> params = new ArrayList<BasicNameValuePair>();
                params.add(new BasicNameValuePair("itemId", itemId));
                params.add(new BasicNameValuePair("modeId", modeId));
                params.add(new BasicNameValuePair("timestamp", timestamp + ""));
                params.add(new BasicNameValuePair("typeId", typeId));
                params.add(new BasicNameValuePair("user_id", uid));

                params.add(new BasicNameValuePair("sign", sign));

                httpRequest =
                        new HttpRequest(context, UrlManager.COLUMN_DOMAIN_PAY, getRequestUrl(), params,
                                PayModel.this);
                httpRequest.setDataParseAdapter(new PayAlipayParser());
                MyTask.runInBackground(httpRequest, true);
            }
        };
        MyTask.runInBackground(runnable, true);

    }
}
